package com.canaan.business.controller.pc.basic;

import com.canaan.business.domain.basic.WmsProduct;
import com.canaan.business.domain.vo.ProductStockVo;
import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.DictUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料主数据Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@Api(tags = "物料管理")
@RestController
@RequestMapping("/basic/product")
public class WmsProductController extends BaseController {
    @Autowired
    private IWmsProductService wmsProductService;

    /**
     * 查询物料主数据列表
     */
    @ApiOperation("查询物料主数据列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsProduct wmsProduct) {
        startPage();
        List<WmsProduct> list = wmsProductService.selectWmsProductList(wmsProduct);
        return getDataTable(list);
    }
    /**
     * 通过仓库查询物料主数据列表
     */
    @ApiOperation("通过仓库查询物料主数据列表")
    @GetMapping("/listByStorehouseNo")
    public TableDataInfo listByStorehouseNo(ProductStockVo wmsProduct) {
        startPage();
        List<WmsProduct> list = wmsProductService.listByStorehouseNo(wmsProduct);
        return getDataTable(list);
    }
    /**
     * 导出物料主数据列表
     */
    @ApiOperation(value = "导出物料主数据列表")
    @Log(title = "物料数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsProduct wmsProduct) {
        List<WmsProduct> list = wmsProductService.selectWmsProductList(wmsProduct);
        List<WmsProduct> listp = Lists.newArrayList();
        for (WmsProduct product : list) {
            String productType = DictUtils.getDictLabel("basic_product_type", product.getProductType());
            product.setProductType(productType);
            String packageType = DictUtils.getDictLabel("package_type", product.getPackageType());
            product.setPackageType(packageType);
            listp.add(product);
        }
        ExcelUtil<WmsProduct> util = new ExcelUtil<WmsProduct>(WmsProduct.class);
        util.exportExcel(response, listp, "物料数据");
    }

    /**
     * 获取物料主数据详细信息
     */
    @ApiOperation(value = "获取物料主数据详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wmsProductService.selectWmsProductById(id));
    }

    /**
     * 根据物料编号获取物料主数据详细信息
     */
    @ApiOperation(value = "根据物料编号获取物料主数据详细信息")
    @GetMapping(value = "/getByProductNo/{productNo}")
    public AjaxResult getInfoByProductNo(@PathVariable("productNo") String productNo) {
        return success(wmsProductService.selectWmsProductByProductNo(productNo));
    }

    /**
     * 新增物料主数据
     */
    @ApiOperation(value = "新增物料主数据")
    @Log(title = "物料主数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsProduct wmsProduct) {
        wmsProduct.setCreateBy(getUsername());
        return toAjax(wmsProductService.insertWmsProduct(wmsProduct));
    }

    /**
     * 修改物料主数据
     */
    @ApiOperation(value = "修改物料主数据")
    @Log(title = "物料主数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsProduct wmsProduct) {
        wmsProduct.setCreateBy(getUsername());
        return toAjax(wmsProductService.updateWmsProduct(wmsProduct));
    }

    /**
     * 删除物料主数据
     */
    @ApiOperation(value = "删除物料主数据")
    @Log(title = "物料主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wmsProductService.deleteWmsProductByIds(ids));
    }


    @ApiOperation(value = "物料主数据导出模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsProduct> util = new ExcelUtil<WmsProduct>(WmsProduct.class);
        util.hideColumn("createByName","createTime","updateByName","updateTime");
        util.importTemplateExcel(response, "物料数据");
    }

    @ApiOperation(value = "物料主数据导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsProduct> util = new ExcelUtil<WmsProduct>(WmsProduct.class);
        List<WmsProduct> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = wmsProductService.importProduct(userList, updateSupport, operName);
        return success(message);
    }
}
